package api;

import model.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;



//注销操作的后端
@WebServlet("/logout")
public class LogoutServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        获取到用户登录时创建的会话
        HttpSession session = req.getSession(false);
        if (session == null) {
//            未登录状态
            resp.setContentType("text/html; charset=utf8");
            resp.getWriter().write("用户未登录,无法注销");
            return;
        }
//        删除session中保存的用户信息
        session.removeAttribute("user");
//        重定向跳转到登录界面
        resp.sendRedirect("login.html");
    }
}
